package com.mysterymall.Service.Impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mysterymall.Common.Comp.HttpStatus;
import com.mysterymall.Common.Utils.Result;
import com.mysterymall.Mapper.SlideshowMapper;
import com.mysterymall.Model.Dto.SlideshowDto;
import com.mysterymall.Model.Entity.Slideshow;
import com.mysterymall.Service.SlideshowService;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName com.mysterymall.Service.Impl
 * @Description
 * @Author HuangWX
 * @Time 2023/11/20  16:16
 */
@Service
public class SlideshowServiceImpl implements SlideshowService {


    private final SlideshowMapper slideshowMapper;

    /**
     * 构造器注入
     *
     * @param slideshowMapper 轮播图mapper
     */
    public SlideshowServiceImpl(SlideshowMapper slideshowMapper) {
        this.slideshowMapper = slideshowMapper;
    }

    @Override
    public Result getSlideshow() {

        // 权限判断
        if (!StpUtil.hasPermission("slideshow:select:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        LambdaQueryWrapper<Slideshow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Slideshow> slideshowList = slideshowMapper.selectList(lambdaQueryWrapper);

        return Result.success("message.querySuccess", slideshowList);
    }

    @Override
    public Result addSlideshow(SlideshowDto slideshowDto) {

        // 权限判断
        if (!StpUtil.hasPermission("slideshow:add:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }

        // 图片处理
        this.imagesControls(slideshowDto);

        return Result.success("message.addSuccess");
    }

    @Override
    public Result updateSlideshow(SlideshowDto slideshowDto) {

        // 权限判断
        if (!StpUtil.hasPermission("slideshow:update:*")) {
            return new Result(HttpStatus.UNAUTHORIZED, "message.powerError");
        }
        LambdaQueryWrapper<Slideshow> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Slideshow::getType, slideshowDto.getType());

        slideshowMapper.delete(lambdaQueryWrapper);

        // 图片处理
        this.imagesControls(slideshowDto);


        return Result.success("message.updateSuccess");
    }

    @Override
    public Result getShowSlideshow(HttpServletRequest request) {


        LambdaQueryWrapper<Slideshow> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Slideshow> slideshowList = slideshowMapper.selectList(lambdaQueryWrapper);


        List<String> imagesOne = new ArrayList<>();
        List<String> imagesTwo = new ArrayList<>();
        for (Slideshow slideshow :
                slideshowList) {
            switch ((String) request.getAttribute("Lang")) {
                case "zh":
                    String[] images = slideshow.getImagesZh().split("_");
                    for (String image :
                            images) {
                        if (slideshow.getType().equals("1")) {
                            imagesOne.add(image);
                        } else {
                            imagesTwo.add(image);
                        }
                    }
                    slideshow.setImagesZh(null);
                    break;
                case "ms":
                    String[] images1 = slideshow.getImagesMs().split("_");
                    for (String image :
                            images1) {
                        if (slideshow.getType().equals("1")) {
                            imagesOne.add(image);
                        } else {
                            imagesTwo.add(image);
                        }
                    }
                    slideshow.setImagesZh(null);
                    break;
                case "en":
                    String[] images2 = slideshow.getImagesEn().split("_");
                    for (String image :
                            images2) {
                        if (slideshow.getType().equals("1")) {
                            imagesOne.add(image);
                        } else {
                            imagesTwo.add(image);
                        }
                    }
                    slideshow.setImagesZh(null);
                    break;
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("one", imagesOne);
        map.put("two", imagesTwo);
        return Result.success("message.querySuccess", map);
    }

    /**
     * 图片处理
     *
     * @param slideshowDto 轮播图dto
     */
    public void imagesControls(SlideshowDto slideshowDto) {

        Slideshow slideshow = new Slideshow();
        StringBuilder[] stringBuilder = {new StringBuilder(), new StringBuilder(), new StringBuilder()};

        for (String image :
                slideshowDto.getImagesZh()) {
            stringBuilder[0].append("_").append(image);
        }

        for (String image :
                slideshowDto.getImagesMs()) {
            stringBuilder[1].append("_").append(image);
        }

        for (String image :
                slideshowDto.getImagesEn()) {
            stringBuilder[2].append("_").append(image);
        }
        slideshow.setImagesZh(stringBuilder[0].substring(1, stringBuilder[0].length()));
        slideshow.setImagesMs(stringBuilder[1].substring(1, stringBuilder[1].length()));
        slideshow.setImagesEn(stringBuilder[2].substring(1, stringBuilder[2].length()));
        if (slideshowDto.getType() == 1) {
            slideshow.setType("1");
            slideshow.setName("商城头部");
        } else {
            slideshow.setType("2");
            slideshow.setName("商城中部");
        }
        slideshowMapper.insert(slideshow);
    }
}
